package com.adee.gulimall.product.app;

import com.adee.gulimall.common.utils.PageUtils;
import com.adee.gulimall.util.utils.R;
import com.adee.gulimall.product.entity.BrandEntity;
import com.adee.gulimall.product.entity.CategoryBrandRelationEntity;
import com.adee.gulimall.product.entity.CategoryEntity;
import com.adee.gulimall.product.service.BrandService;
import com.adee.gulimall.product.service.CategoryBrandRelationService;
import com.adee.gulimall.product.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 品牌分类关联
 *
 * @author adee
 * @email 609980188@qq.com
 * @date 2022-08-19 16:28:49
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }

    // 查询某个分类下的品牌列表
    @RequestMapping("/brands/list")
    // @RequiresPermissions("product:categorybrandrelation:list")
    // public R brandsList(@RequestParam (value = "catId", required = true) Long catId){
    public R brandsList(@RequestParam Map<String, Object> params){
        Long catId = Long.parseLong((String)params.get("catId"));
        List<BrandEntity> list = categoryBrandRelationService.queryListByCatId(catId);

        return R.ok().put("data", list);
    }

    // 查询某个分类下的品牌列表 分页
    @RequestMapping("/brands/list/page")
    // @RequiresPermissions("product:categorybrandrelation:list")
    public R brandsListByPage(@RequestParam Map<String, Object> params){
        Long catId = Long.parseLong((String)params.get("catId"));
        PageUtils page = categoryBrandRelationService.queryPageByCatId(params);

        return R.ok().put("page", page);
    }

    // 根据brandId查询全部关联的catId
    @RequestMapping("/category/list/{brandId}")
    // @RequiresPermissions("product:categorybrandrelation:list")
    public R list(@PathVariable("brandId") Long brandId){
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<CategoryBrandRelationEntity>();
        wrapper.eq("brand_id", brandId);
        List<CategoryBrandRelationEntity> list = categoryBrandRelationService.list(wrapper);

        return R.ok().put("data", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity rela){
        QueryWrapper<CategoryEntity> w1 = new QueryWrapper<>();
        w1.eq("cat_id", rela.getCatelogId());
        QueryWrapper<BrandEntity> w2 = new QueryWrapper<>();
        w2.eq("brand_id", rela.getBrandId());
        CategoryEntity categoryEntity = categoryService.getOne(w1);
        BrandEntity brandEntity = brandService.getOne(w2);
        rela.setCatelogName(categoryEntity.getName());
        rela.setBrandName(brandEntity.getName());
        categoryBrandRelationService.save(rela);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
